package dev.projectnightfall.graphics.states;

import dev.projectnightfall.graphics.RenderEngine;

import net.oscuro87.dev.slickaddin.entities.menu.Menu;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Vector2f;
import org.newdawn.slick.state.StateBasedGame;

public class MainMenuState extends AGameState {
    // Menu items

    Menu mainmenu;

    /**
     * Constructor to the Main Menu Gamestate.
     *
     * @param stateID The ID you want to assign to the Main Menu State.
     */
    public MainMenuState(int stateID) {
        super(stateID);
    }

    /**
     * The state initialization code comes here.
     *
     * @param container The game screen itself.
     * @param game The game logic.
     * @throws SlickException
     */
    @Override
    public void init(GameContainer container, StateBasedGame game)
            throws SlickException {
        mainmenu = new Menu(container.getGraphics(),
                container, new Vector2f(100, 125), "Main Menu");
        mainmenu.setMenuTitleUnderline(true);
        mainmenu.addMenuItem("mm_startgame", "Start Game");
        mainmenu.addMenuItem("mm_options", "Options");
        mainmenu.addMenuItem("mm_quit", "Quit");
    }

    /**
     * The state drawing code comes here.
     *
     * @param container The game screen.
     * @param game The game logic.
     * @param g Graphics object to be able to draw on the screen.
     * @throws SlickException
     */
    @Override
    public void render(GameContainer container, StateBasedGame game, Graphics g)
            throws SlickException {
        g.clear();
        mainmenu.doDraw();
    }

    /**
     * Updates the game X times per second (depending on the FPS)
     *
     * @param container The game screen.
     * @param game The game logic.
     * @param delta Time elapsed since the last update.
     * @throws SlickException
     */
    @Override
    public void update(GameContainer container, StateBasedGame game, int delta)
            throws SlickException {
        Input in = container.getInput();

        if (Mouse.isButtonDown(0)) {
            String selectedItem = mainmenu.getSelectedItemName();
            if (selectedItem == "mm_startgame") {
                game.enterState(RenderEngine.GAMEPLAYSTATE);
            }
            if (selectedItem == "mm_options") {
                game.enterState(RenderEngine.OPTIONSMENUSTATE);
            }
            if (selectedItem == "mm_quit") {
                container.exit();
            }
        }

        if (in.isKeyPressed(Keyboard.KEY_DOWN)) {
            mainmenu.selectNextItem();
        }
        if (in.isKeyPressed(Keyboard.KEY_UP)) {
            mainmenu.selectPreviousItem();
        }
        if (in.isKeyPressed(Keyboard.KEY_RETURN)) {
            String selectedItem = mainmenu.getSelectedItemName();
            if (selectedItem == "mm_startgame") {
                game.enterState(RenderEngine.GAMEPLAYSTATE);
            }
            if (selectedItem == "mm_options") {
                game.enterState(RenderEngine.OPTIONSMENUSTATE);
            }
            if (selectedItem == "mm_quit") {
                container.exit();
            }
        }

        mainmenu.doUpdate(container);
    }
}
